<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">

<f:view encoding="ISO-8859-1">
	<h:head>
		<title>Distribuidores cadastrados</title>
		<h:outputStylesheet library="css" name="styles.css" />
		<h:outputScript library="js" name="scripts.js" />
		<script type="text/javascript">
			function validarForm() {
				if (!validaCnpj(document.getElementById('frmDist:txtDistCNPJ').value)) {
					alert("CNPJ inválido. Tente novamente.");
					return false;
				} else {
					return true;
				}
			}
		</script>
	</h:head>
	<h:body>
		<h:form id="frmDist" styleClass="frm"
			rendered="#{loginBean.isLogged() and loginBean.hasPermission('CadGeneral')}">
			
			<ui:include src="../../templates/menu.xhtml" />
			
			<p:growl id="messages" showDetail="true" sticky="true"/>
			
			<p:panel id="cadPanelDistributor" header="Cadastro de distribuidores" styleClass="cadPanelPrincipal">
				<h:panelGrid id="panelGrid" columns="4" cellspacing="5">
					<h:outputLabel id="lblDistName" value="Distribuidor: "
						styleClass="outPutLabel" />
					<p:inputText id="txtDistName" label="Distribuidor"
						styleClass="inputText" value="#{distributorBean.name}">
					</p:inputText>
					<h:outputLabel id="lblDistCNPJ" value="CNPJ: "
						styleClass="outPutLabel" />
					<p:inputMask id="txtDistCNPJ" label="CNPJ" styleClass="inputText" mask="99.999.999/9999-99"
						value="#{distributorBean.cnpj}">
					</p:inputMask>
					
					<h:outputLabel id="lblDistEmail" value="E-mail: "
						styleClass="outPutLabel" />
					<p:inputText id="txtDistEmail" label="E-mail"
						styleClass="inputText" value="#{distributorBean.email}">
					</p:inputText>
					<h:outputLabel id="lblDistSegment" value="Segmento: "
						styleClass="outPutLabel" />
					<p:inputText id="txtDistSegment" label="Segmento"
						styleClass="inputText" value="#{distributorBean.segment}">
					</p:inputText>
					
					<h:outputLabel id="lblDistCEP" value="CEP: "
						styleClass="outPutLabel" />
					<p:inputMask id="txtDistCEP" label="CEP" styleClass="inputText" mask="99999-999"
						value="#{addressBean.zip}">
						<f:ajax event="blur" listener="#{addressBean.loadAddresses}"
							render="txtDistEstate txtDistCity txtDistNeighborhood selDistStreet" />
					</p:inputMask>
					<h:outputLabel id="lblDistEstate" value="Estado: "
						styleClass="outPutLabel" />
					<p:inputText id="txtDistEstate" label="Estado"
						styleClass="inputText" value="#{addressBean.estate.name}">
					</p:inputText>
					<h:outputLabel id="lblDistCity" value="Cidade: "
						styleClass="outPutLabel" />
					<p:inputText id="txtDistCity" label="Cidade" styleClass="inputText"
						value="#{addressBean.city.name}">
					</p:inputText>
					<h:outputLabel id="lblDistNeighborhood" value="Bairro: "
						styleClass="outPutLabel" />
					<p:inputText id="txtDistNeighborhood" label="Bairro"
						styleClass="inputText"
						value="#{addressBean.neighborhood.name}">
					</p:inputText>
					<h:outputLabel id="lblDistStreet" value="Rua: "
						styleClass="outPutLabel" />
					<p:selectOneMenu id="selDistStreet" value="#{distributorBean.addCode}">
						<f:selectItem itemLabel="Selecione o logradouro" itemValue="" />  
						<f:selectItems value="#{addressBean.addressList}"  var="addressItem"
							itemValue="#{addressItem.adcode}" itemLabel="#{addressItem.name}" />
						<p:ajax listener="#{distributorBean.selDistChanged}"/>
					</p:selectOneMenu>
					<h:outputLabel id="lblDistAddressNumber" value="Número: "
						styleClass="outPutLabel" />
					<p:inputText id="txtDistAddressNumber" label="Número"
						styleClass="inputText"
						value="#{distributorBean.addNumber}">
					</p:inputText>
					<p:commandButton id="cmdButtonRegister" value="Cadastrar" onclick="return validarForm();" actionListener="#{distributorBean.onRegisterClick}" update=":frmDist:tblDistributors :frmDist:messages" icon="ui-icon-disk"/>
				</h:panelGrid>
				<p:dataTable id="tblDistributors" var="dis" value="#{distributorBean.distributorList}" rowKey="#{dis.dicode}"	selection="#{distributorBean.distributor}" selectionMode="single">
					
					<p:column headerText="CÓDIGO">
						#{dis.dicode}
					</p:column>
					<p:column headerText="DISTRIBUIDOR">
						#{dis.person.name}
					</p:column>
					<p:column headerText="CNPJ">
						#{dis.formattedDocument}
					</p:column>
					<p:column headerText="ESTADO">
						#{dis.person.address.neighborhood.city.estate.name}
					</p:column>
					<p:column headerText="CIDADE">
						#{dis.person.address.neighborhood.city.name}
					</p:column>
					<p:column headerText="BAIRRO">
						#{dis.person.address.neighborhood.name}
					</p:column>
					<p:column headerText="ENDEREÇO">
						#{dis.person.address.name}
					</p:column>
					<p:column headerText="NÚMERO">
						#{dis.person.addressNumber}
					</p:column>
					<p:column headerText="CEP">
						#{dis.person.address.formattedZip}
					</p:column>
					
					<f:facet name="footer">
						<p:commandButton id="updButton" value="Atualizar" icon="ui-icon-pencil"/>
						<p:commandButton id="delButton" value="Remover" onclick="confDialog.show();" icon="ui-icon-trash"/>
					</f:facet>
					
				</p:dataTable>
			</p:panel>
			
			<p:confirmDialog id="confirmDialog" message="Você tem certeza que quer apagar o distribuidor selecionado?"
				header="Apagar gênero" severity="alert" widgetVar="confDialog">
				<p:commandButton id="deleteButton" value="Sim" update="messages :frmDist:tblDistributors" oncomplete="confDialog.hide();" actionListener="#{distributorBean.onDeleteClick}" />
                <p:commandButton id="cancelButton" value="Cancelar" onclick="confDialog.hide();" type="button" />
            </p:confirmDialog>
			
			
		</h:form>
		<h:form	rendered="#{!loginBean.isLogged() or !loginBean.hasPermission('CadGeneral')}">
			<h1 align="center">
				<h:outputText value="Acesso não autorizado a esta página!" />
			</h1>
			<h1 align="center">
				<h:graphicImage value="#{resource['images/general/forbidden.png']}" />
			</h1>
		</h:form>
		<ui:include src="../../templates/footer.xhtml" />
	</h:body>
</f:view>
</html>